package cn.bugstack.chatgpt.data.test;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

@Slf4j
public class ApiTest1 {

    // 「沙箱环境」应用ID - 您的APPID，收款账号既是你的APPID对应支付宝账号。获取地址；https://open.alipay.com/develop/sandbox/app
    public static String app_id = "9021000135656055";
    // 「沙箱环境」商户私钥，你的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDFKhmTU70mAm5HvgUzxPOg248yD/91/4XnqCfXLFi0nfta406s4fg9WZUk88cmPt8HZLc7RIivBe3lCvhgTbfOfVdkNX9Gwx9TFde96wMa5xV1IVyaxU+cxhg/sM+/xnWWip3pqrQFIyRDEQ1yCL8dVsJA/qTXX9Hp16PE8oIQZCyoUz3tdO5ko58Fl74UkjxgkoUjxtoocJVKLyj2muH21Q/6dNaC+Rc7aHMH30F3zOMEJtYFQcZgBgHGXQ0SjwfWFxIW5AE4L5hqGwsNNadk+sf0kGetalpb2i2ZfN7nuSx31fIxohvD0bwFzad4d2632U0uOXVQpq4WYcf7Izq7AgMBAAECggEAciZArmph7TqlDIY4TtOtVakoS4y1k2R/6zIQ9uPm65M3rlRB/ZfKeAKAdedpmnv19jraVscD94kQRXZHfMIiM7ufuBW8/YQEg772rC7/GjlF1A2fpr+rqxGJgn5kmRxWgfT5HgrFtN+2o9mgli/4Lg1H5AdJ+qOwTZmaaHEa2k+Pt6HfuvwPIoP7437fTrrdY163LIMYpqBAVRPdVcr9c0OAWi2+3RUP2iznTsSEs6LggaeWVB1q2sFxY3sYFpK7810IjpWpUyAvb6EFr+YvGAZoLf0ApcfghFrn6kXbtugBYI/9fbswHv85EMYYH/Tr5lIr5CJ7SvQpKtcfz+s6QQKBgQD294hRfqtDOZY/9VYSGmngiR1aUZwMgNhJdU18aOUR5/l8a63But5J0NHe2vAdOHlNRcv6HUciYGSgk0MIeV5U9qkFjwM0VIeDBrHZLZVKrR4+cenAIChlMJQzoCJn68FVy8QtN3YOPwekKsO3nRIhDNEDWz+mPdLyI6k8xUL3mwKBgQDMYD5SN+jOF+PaxA0bhwNIjOxcuE+e02yAgL0kqJaBU3YOtJf+bGMRzwgpIWQqHAQH0VAxMk/jga0T9EOP2eiF3rAU/nmJ6Cj8urvXzVfP6F5mu90vl54QsfCa9kxsp/9P8elXXjKt6ABN+tKbFMIRfz599mv92Oh5OH0/QLtLYQKBgAHdhsMO0N5WHgNnp/nFB02x223mNeaGRpvvbeFlrW1AMks1SP+wmKM4F422W5/s4EWsJElEVVg2ICxyUExNFmBP6KDpHp4TkV50+3zncjw5KIRSOfDAjW2OO2BUck4UosYyHs40oAilgzpDNG07m3pybVW4J7fquaAoksuptcLHAoGBAMFm6lLfuZ0eGm9xS34bO84yHZJ4kFAafvjz2wmPyC1GD9hGqfcIFpBJvM0/XgxDSsUjHKIRnGwM0P6rDixY56OXYkXbsqQAYJkZFJ1ZIpoKqRrK+LsK3XerC6d5IEVKjV7X7BrTd13+yBsDGr3K3QO1yCtnQDaORyA/hraOyS7BAoGBAJgcTjJ18lj3KGBJoiByZehZG1R8fUR0NDrMTFWLcQg3fKaK+7HNHlbWFd1kuxo8vPglRtH77ZzpWGkPkj9XXOSobdTtnBWWz01GbGkCgn0dG2kZo7sL2vETjKw0VUX7QvGWbihhlotlZv2x5Ou1rvrLPm3lDQzfoPJOSxi/MGOx";
    // 「沙箱环境」支付宝公钥
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgXlseiEm0qUl6def4QHN2aF+R1SMEZyUc62bhtzjrvTSpdDokUT1C0tTqB2pewIvEzwC0gJuEZo+sE9rhObVsFwLgNh8mOOt1izIpYbEWDY7hk5X5DrMGVNgk1n9MICS+Yxb3u3gK5Fi5q5iRNMmh/PCO5kB4NgtnMpPBwTdoOzobUjWnLcdY/XUydQlNoGHZMDXQun7xNsh4+oyY0z6zz3og/+KHWN0AGCpW+nE6JP+pGb4guLzECoH6G4XjbLTPd2vs5zusio10f5OI1+cXRtScJWGaYFxaHHUx5rsujcD2KsNUVhhQq6ZGTxS+vecQ9BFmZOf5SKznCvmM1IXsQIDAQAB";
    // 「沙箱环境」服务器异步通知回调地址
    public static String notify_url = "https://xfg.natapp.cn/api/v1/alipay/alipay_notify_url";
    // 「沙箱环境」页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "https://gaga.plus";
    // 「沙箱环境」
    public static String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 签名方式
    public static String sign_type = "RSA2";
    // 字符编码格式
    public static String charset = "utf-8";

    private AlipayClient alipayClient;

    @Before
    public void init() {
        this.alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id,
                merchant_private_key,
                "json",
                charset,
                alipay_public_key,
                sign_type);
    }

    @Test
    public void test_aliPay_pageExecute() throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();  // 发送请求的 Request类
        request.setNotifyUrl(notify_url);
        request.setReturnUrl(return_url);

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "daniel82AAAA000032333361X06");  // 我们自己生成的订单编号
        bizContent.put("total_amount", "0.01"); // 订单的总金额
        bizContent.put("subject", "测试商品");   // 支付的名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");  // 固定配置
        request.setBizContent(bizContent.toString());

        String form = alipayClient.pageExecute(request).getBody();
        log.info("测试结果：{}", form);

        /**
         * 会生成一个form表单；
         * <form name="punchout_form" method="post" action="https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8&method=alipay.trade.page.pay&sign=CAAYYDIbvUNRDvY%2B%2BF5vghx2dL9wovodww8CK0%2FferNP1KtyXdytBVLdZKssaFJV%2B8QksVuKlU3qneWhWUuI7atLDgzpussJlJhxTMYQ3GpAfOP4PEBYQFE%2FORemzA2XPjEn88HU7esdJdUxCs602kiFoZO8nMac9iqN6P8deoGWYO4UAwE0RCV65PKeJTcy8mzhOTgkz7V018N9yIL0%2BEBf5iQJaP9tGXM4ODWwFRxJ4l1Egx46FNfjLAMzysy7D14LvTwBi5uDXV4Y%2Bp4VCnkxh3Jhkp%2BDP9SXx6Ay7QaoerxHA09kwYyLQrZ%2FdMZgoQ%2BxSEOgklIZtYj%2FLbfx1A%3D%3D&return_url=https%3A%2F%2Fgaga.plus&notify_url=http%3A%2F%2Fngrok.sscai.club%2Falipay%2FaliPayNotify_url&version=1.0&app_id=9021000132689924&sign_type=RSA2&timestamp=2023-12-13+11%3A36%3A29&alipay_sdk=alipay-sdk-java-4.38.157.ALL&format=json">
         * <input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;100001001&quot;,&quot;total_amount&quot;:&quot;1.00&quot;,&quot;subject&quot;:&quot;测试&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE_PAY&quot;}">
         * <input type="submit" value="立即支付" style="display:none" >
         * </form>
         * <script>document.forms[0].submit();</script>
         */
    }

    /**
     * 查询订单
     */
    @Test
    public void test_alipay_certificateExecute() throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
        bizModel.setOutTradeNo("daniel82AAAA000032333361X03");
        request.setBizModel(bizModel);

        String body = alipayClient.certificateExecute(request).getBody();
        log.info("测试结果：{}", body);
    }

    /**
     * 退款接口
     */
    @Test
    public void test_alipay_refund() throws AlipayApiException {
        AlipayTradeRefundRequest request =new AlipayTradeRefundRequest();
        AlipayTradeRefundModel refundModel =new AlipayTradeRefundModel();
        refundModel.setOutTradeNo("daniel82AAAA000032333361X03");
        refundModel.setRefundAmount("1.00");
        refundModel.setRefundReason("退款说明");
        request.setBizModel(refundModel);

        AlipayTradeRefundResponse execute = alipayClient.execute(request);
        log.info("测试结果：{}", execute.isSuccess());
    }

}
